import { registerGateways, registerHandlers, run, type Handler } from "encore.dev/internal/codegen/appinit";

{{#each gateways}}
import { {{bind_name}} as {{encoreNameToIdent encore_name}}GW } from {{toJSON import_path}};
{{/each}}
{{#each endpoints}}
import { {{name}} as {{encoreNameToIdent service_name}}_{{name}}Impl{{@index}} } from {{toJSON import_path}};
{{/each}}
{{#each subscriptions}}
import {{toJSON import_path}};
{{/each}}
{{#each services}}
{{#if import_path}}
import * as {{encoreNameToIdent name}}_service from {{toJSON import_path}};
{{/if}}
{{/each}}

const gateways: any[] = [
{{#each gateways}}
    {{encoreNameToIdent encore_name}}GW,
{{/each}}
];

const handlers: Handler[] = [
{{#each endpoints}}
    {
        apiRoute: {
            service:           {{toJSON service_name}},
            name:              {{toJSON name}},
            handler:           {{encoreNameToIdent service_name}}_{{name}}Impl{{@index}},
            raw:               {{toJSON raw}},
            streamingRequest:  {{toJSON streaming_request}},
            streamingResponse: {{toJSON streaming_response}},
        },
        endpointOptions: {{toJSON endpoint_options}},
        middlewares: {{#with (lookup (lookup ../services service_name) "import_path")}}{{encoreNameToIdent ../service_name}}_service.default.cfg.middlewares || {{/with}}[],
    },
{{/each}}
];

registerGateways(gateways);
registerHandlers(handlers);

await run(import.meta.url);
